package cn.tedu.oa.sys.controller;

import cn.tedu.oa.common.annotation.LogAnnotation;
import cn.tedu.oa.common.util.ShiroUtils;
import cn.tedu.oa.common.vo.JsonResult;
import cn.tedu.oa.sys.entity.Config;
import cn.tedu.oa.sys.entity.Department;
import cn.tedu.oa.sys.service.ConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sys/config/")
public class ConfigController {
    @Autowired
    private ConfigService configService;

    /**/
    @RequestMapping("config_ui")
    public String doConfigUI() {
        return "sys/config/config_ui";
    }

    @RequestMapping("config_edit")
    public String doConfigEdit(){
        return  "sys/config/config_edit";
    }

    @RequestMapping("config_update")
    public String doConfigUpdate(){
        return  "sys/config/config_update";
    }

    @LogAnnotation("添加配置")
    @ResponseBody
    @RequestMapping("add")
    public JsonResult add(Config config){
        configService.add(config);
        return  new JsonResult("add ok");
    }

    /*显示数据列表*/
    @LogAnnotation("配置分页查询")
    @RequestMapping("get")
    @ResponseBody
    public JsonResult get(String criteria,Integer pageNo,Integer pageSize) {
        List<Config> list = configService.getConfigPageList(criteria, pageNo, pageSize);
        int rowCount = configService.getRowCount(criteria);
        JsonResult result = new JsonResult();
        result.setCount(rowCount);
        result.setData(list);
        return result;
    }

    @LogAnnotation("删除配置信息")
    @RequestMapping("deleteById")
    @ResponseBody
    public JsonResult delete(String id){
        int id1 = Integer.parseInt(id);
        configService.delete(id1);
        return new JsonResult("delete ok");
    }

    @LogAnnotation("删除多个配置信息")
    @RequestMapping("deleteByIds")
    @ResponseBody
    public JsonResult deleteByIds(Integer...ids){
        System.out.println(ids);
        configService.deleteByIds(ids);
        return new JsonResult("delete ok");
    }

    @LogAnnotation("修改配置信息")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update (Config config){
        config.setUpdateUser(ShiroUtils.getPrincipal().getName());
        config.setUpdateTime(new Date());
        configService.update(config);
        return new JsonResult("update ok");
    }
}
